'use strict';

const { Controller } = require('egg');



class regController extends Controller {
    async reg() {

        //登录
        const { ctx } = this;

        let formUn = this.ctx.request.body.user.username;
        let formAcc = this.ctx.request.body.user.account;

        //生成token
        let token = this.app.jwt.sign(formUn, this.app.config.jwt.secret);

        //按照username到数据库里检测
        let userOne = await this.app.model.User.findAll({ where: { username: formUn } });

        //如果找到该用户名，提醒用户名存在
        if (userOne.length === 1) {
            this.ctx.body = {
                code: 400,
                token: ''
            }
            // await this.app.model.User.create({ username: formUn, account: formAcc });
        } else {
            await this.app.model.User.create({ username: formUn, account: formAcc });
            this.ctx.body = {
                code: 200,
                token: token
            }

        }

    }

}

module.exports = regController;
